/**
 * 
 */
package de.cbf.cam.dsa.model.entities.items.transfers;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.eclipse.swt.dnd.ByteArrayTransfer;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.TransferData;

import de.cbf.cam.dsa.model.entities.ArmorPart;

/**
 * @author Nebelritter
 *
 */
public class ArmorPartTransfer extends ByteArrayTransfer implements ITransfer{
	private static final String TYPENAME = "armorPartTransfer";

	private static final int TYPEID = registerType(TYPENAME);

	private static ArmorPartTransfer instance = new ArmorPartTransfer();
	
	public static ArmorPartTransfer getInstance() {
		return instance;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.swt.dnd.Transfer#getTypeIds()
	 */
	@Override
	protected int[] getTypeIds() {
		return new int[] { TYPEID };
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.swt.dnd.Transfer#getTypeNames()
	 */
	@Override
	protected String[] getTypeNames() {
		return new String[] { TYPENAME };		
	}	

	boolean checkItem(Object object) {
		return (object != null && object instanceof ArmorPart);
	}

	protected boolean validate(Object object) {
		return checkItem(object);
	}
	public void javaToNative(Object object, TransferData transferData) {
	      if (!checkItem(object) || !isSupportedType(transferData)) {
	        DND.error(DND.ERROR_INVALID_DATA);
	      }
	      ArmorPart armorPart = (ArmorPart) object;
	      try {
	    	  // write data to a byte array and then ask super to convert to
	    	  // pMedium
	    	  ByteArrayOutputStream out = new ByteArrayOutputStream();
	    	  DataOutputStream writeOut = new DataOutputStream(out);
	    	 
	    	  writeArmorPart(armorPart, writeOut);
	    	  
	        
	    	  byte[] buffer = out.toByteArray();
	    	  writeOut.close();
	    	  
	    	  super.javaToNative(buffer, transferData);
	      } catch (IOException e) {
	      }
	    }

	public Object nativeToJava(TransferData transferData) {
	  	if (isSupportedType(transferData)) {
	  		byte[] bufferAll = (byte[]) super.nativeToJava(transferData);
	  		if (bufferAll == null){
	  			return null;
	  		}
	  		
	  		try {
	  			ByteArrayInputStream in = new ByteArrayInputStream(bufferAll);
	  			DataInputStream readIn = new DataInputStream(in);
	  			ArmorPart armorPart= new ArmorPart();
	  			while (readIn.available() > 20) {
	  				
	  				 armorPart = readArmorPart(armorPart,readIn);
	  			}
		  		readIn.close();
		  		return armorPart;
	        } catch (IOException ex) {	        	
	          return null;
	        }        
	    }
    return null;
	}
	  
	static void writeArmorPart(ArmorPart armorPart,DataOutputStream writeOut) throws IOException{
		ItemTransfer.writeItem(armorPart, writeOut);
		//write armorPart specifics
		int stars = armorPart.getStars();
		writeOut.writeInt(stars);
		
		int[]rsZones = armorPart.getRsZones();
		int rsSize = rsZones.length;
		writeOut.writeInt(rsSize);
		for(int i= 0;i<rsSize;i++){
			int rsAtZone = rsZones[i];			
			writeOut.writeInt(rsAtZone);
		}
		int[]structureZones = armorPart.getStructureZones();
		int structureSize = structureZones.length;
		writeOut.writeInt(structureSize);
		for(int i= 0;i<structureSize;i++){
			int structureAtZone = structureZones[i];
			writeOut.writeInt(structureAtZone);
		}
	}
	  
	static ArmorPart readArmorPart(ArmorPart armorPart,DataInputStream input) throws IOException{
		ItemTransfer.readItem(armorPart, input);
		int stars = input.readInt();
		armorPart.setStars(stars);
		
		int rsSize = input.readInt();
		int[]rs = new int[rsSize];
		for(int i = 0;i<rsSize;i++){
			rs[i]=input.readInt();
		}
		armorPart.setRsZones(rs);
		
		int structureSize = input.readInt();
		int[]structure = new int[structureSize];
		for(int i = 0;i<structureSize;i++){
			structure[i]=input.readInt();
		}
		armorPart.setStructureZones(structure);
		return armorPart;
	}
	@Override
	public String toString() {
		return getClass().getSimpleName();
	}
}
